//
//  EFAAppModel.m
//  efairy
//
//  Created by XiangDongMBP on 2017/8/12.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAAppModel.h"
#import "GlobalHeader.h"
#import "EFADeviceModel.h"
#import "NSString+XDNSDate.h"

@implementation EFAPjtAddViewCfg
@end

@implementation EFAPjtListItem
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }
@end
@implementation EFAProjectModel
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }

+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"project_list" : [EFAPjtListItem class]};
}
- (NSArray<EFAPjtListItem *> *)selPjtArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.project_list.count];
    [self.project_list enumerateObjectsUsingBlock:^(EFAPjtListItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected) {
            [mArray addObject:obj];
        }
    }];
    return mArray;
}
- (BOOL)hasSelPjt {
    for (EFAPjtListItem *item in self.project_list) {
        if (item.isSelected) {
            return YES;
        }
    }
    return NO;
}
- (BOOL)totalLoad {
    return (self.project_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.project_list.count == 0;
}
- (void)addMoreWithData:(EFAProjectModel *)temp {
    self.total_rows = temp.total_rows;
    [self.project_list addObjectsFromArray:temp.project_list];
}
@end


#pragma mark - 项目管理详情
@implementation EFAPjtDetUserItem
@end
//@implementation EFAPjtDetDeviceItem
//@end
@implementation EFAPjtDetailModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"gm_list" : [EFAPjtDetUserItem class],
             @"user_list" : [EFAPjtDetUserItem class],
             @"device_list" : [EFADeviceListItem class]};
}
- (BOOL)isGMTotalLoad {
    return self.gm_list.count >= self.project_info.efairyproject_total_gms;
}
- (BOOL)isUserTotalLoad {
    return self.user_list.count >= self.project_info.efairyproject_total_users;
}
- (BOOL)isDeviceTotalLoad {
    return self.device_list.count >= self.project_info.efairyproject_total_devices;
}
- (BOOL)isGMTotalZero {
    return self.gm_list.count == 0;
}
- (BOOL)isUserTotalZero {
    return self.user_list.count == 0;
}
- (BOOL)isDeviceTotalZero {
    return self.device_list.count == 0;
}
- (void)addMoreUserWithDictionary:(NSDictionary *)dic {
    if ([dic[EFAGlobalJsonKeyErrCode] integerValue] == 200) {
        EFAPjtUserModel *temp = [EFAPjtUserModel yy_modelWithDictionary:dic[EFAGlobalJsonKeyResult]];
        [self.user_list addObjectsFromArray:temp.user_list];
        
        self.project_info.efairyproject_total_users = temp.total_rows;
        self.user_list_unfolder = (self.user_list.count == self.project_info.efairyproject_total_users);
    }
}
- (void)addMoreGMWithDictionary:(NSDictionary *)dic {
    if ([dic[EFAGlobalJsonKeyErrCode] integerValue] == 200) {
        EFAPjtUserModel *temp = [EFAPjtUserModel yy_modelWithDictionary:dic[EFAGlobalJsonKeyResult]];
        [self.gm_list addObjectsFromArray:temp.user_list];
        
        self.project_info.efairyproject_total_gms = temp.total_rows;
        self.gm_list_unfolder = (self.gm_list.count == self.project_info.efairyproject_total_gms);
    }
}
- (void)addMoreDeviceWithDictionary:(NSDictionary *)dic {
    if ([dic[EFAGlobalJsonKeyErrCode] integerValue] == 200) {
        EFADeviceModel *temp = [EFADeviceModel yy_modelWithDictionary:dic[EFAGlobalJsonKeyResult]];
        [self.device_list addObjectsFromArray:temp.device_list];
        
        self.project_info.efairyproject_total_devices = temp.total_rows;
        self.device_list_unfolder = (self.device_list.count == self.project_info.efairyproject_total_devices);
    }
}
- (void)gmListWithDictionary:(NSDictionary *)dic isAddMore:(BOOL)addMore {
    if ([dic[EFAGlobalJsonKeyErrCode] integerValue] != 200) {
        return;
    }
    EFAPjtUserModel *temp = [EFAPjtUserModel yy_modelWithDictionary:dic[EFAGlobalJsonKeyResult]];
    if (!temp) {
        return;
    }
    
    if (addMore && self.gm_list) {
        [self.gm_list addObjectsFromArray:temp.user_list];
    } else {
        self.gm_list = temp.user_list;
    }
    self.project_info.efairyproject_total_gms = temp.total_rows;
    self.gm_list_unfolder = (self.gm_list.count == self.project_info.efairyproject_total_gms);
}
- (void)userListWithDictionary:(NSDictionary *)dic isAddMore:(BOOL)addMore {
    if ([dic[EFAGlobalJsonKeyErrCode] integerValue] != 200) {
        return;
    }
    EFAPjtUserModel *temp = [EFAPjtUserModel yy_modelWithDictionary:dic[EFAGlobalJsonKeyResult]];
    if (!temp) {
        return;
    }
    
    if (addMore && self.user_list) {
        [self.user_list addObjectsFromArray:temp.user_list];
    } else {
        self.user_list = temp.user_list;
    }
    self.project_info.efairyproject_total_users = temp.total_rows;
    self.user_list_unfolder = (self.user_list.count == self.project_info.efairyproject_total_users);
}
- (void)deviceListWithDictionary:(NSDictionary *)dic isAddMore:(BOOL)addMore {
    if ([dic[EFAGlobalJsonKeyErrCode] integerValue] != 200) {
        return;
    }
    EFADeviceModel *temp = [EFADeviceModel yy_modelWithDictionary:dic[EFAGlobalJsonKeyResult]];
    if (!temp) {
        return;
    }
    
    if (addMore && self.device_list) {
        [self.device_list addObjectsFromArray:temp.device_list];
    } else {
        self.device_list = temp.device_list;
    }
    self.project_info.efairyproject_total_devices = temp.total_rows;
    self.device_list_unfolder = (self.device_list.count == self.project_info.efairyproject_total_devices);
}
- (void)removeUserAtIndex:(NSInteger)index {
    if (index >= self.user_list.count) {
        return;
    }
    
    [self.user_list removeObjectAtIndex:index];
    --self.project_info.efairyproject_total_users;
}
- (void)removeGMAtIndex:(NSInteger)index {
    if (index >= self.gm_list.count) {
        return;
    }
    [self.gm_list removeObjectAtIndex:index];
    --self.project_info.efairyproject_total_gms;
}
@end


#pragma mark - 项目管理详情-用户列表
@implementation EFAPjtUserModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_users", @"efairyproject_total_gms"],
             @"user_list": @[@"user_list", @"gm_list"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"user_list" : [EFAPjtDetUserItem class]};
}
- (BOOL)totalLoad {
    return (self.user_list.count >= self.total_rows);
}
- (void)addMoreWithData:(EFAPjtUserModel *)temp {
    self.total_rows = temp.total_rows;
    [self.user_list addObjectsFromArray:temp.user_list];
}
@end

#pragma mark - 项目管理详情-选择ICCID
@implementation EFAPjtIotItem

@end
@implementation EFAPjtIotModel
//+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
//    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_users", @"efairyproject_total_gms"],
//             @"user_list": @[@"user_list", @"gm_list"]};
//}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairyiotcard_list" : [EFAPjtIotItem class]};
}
- (BOOL)totalLoad {
    return (self.efairyiotcard_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.efairyiotcard_list.count == 0;
}
- (void)addMoreWithData:(EFAPjtIotModel *)temp {
    self.total_rows = temp.total_rows;
    [self.efairyiotcard_list addObjectsFromArray:temp.efairyiotcard_list];
}
@end

#pragma mark - 报表
@implementation EFARptOviAnaItem
- (NSString *)deviceTypeDes {
    return [[EFADeviceProtocolModel sharedInstance] deviceTypeDescribeWithType:self.device_type];
}
@end
@implementation EFARptOverView
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"overview_analyze_list" : [EFARptOviAnaItem class]};
}
- (NSInteger)device_total_number {
    NSInteger cnt = 0;
    for (EFARptOviAnaItem *item in self.overview_analyze_list) {
        cnt += item.install_number;
    }
    return cnt;
}
- (NSInteger)device_alarm_number {
    NSInteger cnt = 0;
    for (EFARptOviAnaItem *item in self.overview_analyze_list) {
        cnt += item.alarm_number;
    }
    return cnt;
}
- (NSInteger)device_normal_number {
    return self.device_total_number - self.device_alarm_number;
}
@end
@implementation EFARptScaleMapItem
- (NSString *)ptDes {
    NSDictionary *dic = [[EFADeviceProtocolModel sharedInstance] protocolInfoWithPt:self.pt];
    if (dic) {
        return dic[EFADeviceProtocolPtTitle];
    }
    return @"未定义";
}
@end
@implementation EFARptAlarmScaleItem
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"scale_map_list" : [EFARptScaleMapItem class]};
}
- (NSString *)deviceTypeDes {
    return [[EFADeviceProtocolModel sharedInstance] deviceTypeDescribeWithType:self.device_type];
}
- (NSArray<NSDictionary *> *)pieData {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.scale_map_list.count];
//    for (EFARptScaleMapItem *item in self.scale_map_list) {
//        NSArray *temp = @[item.ptDes, @(item.alarm_times)];
//        [mArray addObject:temp];
//    }
    
    // 颜色值
    NSArray *color = @[@"#7CD89E", @"#FFA954", @"#629CE6", @"#E671D7", @"#B28CE5"];
    [self.scale_map_list enumerateObjectsUsingBlock:^(EFARptScaleMapItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger index = idx % color.count;
        NSDictionary *temp = @{@"name" : obj.ptDes,
                               @"color" : color[index],
                               @"y" : @(obj.alarm_times)};
        [mArray addObject:temp];
    }];
    return mArray;
}
@end
@implementation EFARptAlarmDataItem
@end
@implementation EFARptModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"alarm_scale_map_list" : [EFARptAlarmScaleItem class],
             @"alarm_data_list" : [EFARptAlarmDataItem class]};
}
- (NSArray<EFARptScaleMapItem *> *)alarm_total {
    NSMutableArray<EFARptScaleMapItem *> *mArray = [NSMutableArray array];
    for (EFARptAlarmDataItem *item in self.alarm_data_list) {
        BOOL found = NO;
        for (EFARptScaleMapItem *temp in mArray) {
            if (temp.pt == item.efairydevicefiredata_parameter) {
                ++temp.alarm_times;
                found = YES;
                break;
            }
        }
        if (!found) {
            EFARptScaleMapItem *oneNew = [[EFARptScaleMapItem alloc] init];
            oneNew.pt = item.efairydevicefiredata_parameter;
            oneNew.alarm_times = 1;
            [mArray addObject:oneNew];
        }
    }
    return mArray;
}
- (NSString *)reportDescribe {
    NSString *str = [NSString stringWithFormat:@"用电安全监测情况： 所监测的%ld个点位，%ld个正常，%ld个报警。 报警次数总计%ld次",
            (long)self.overview.device_total_number,
            (long)self.overview.device_normal_number,
            (long)self.overview.device_alarm_number,
            (unsigned long)self.alarm_data_list.count];
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.alarm_total.count];
    for (EFARptScaleMapItem * item in self.alarm_total) {
        NSString *temp = [NSString stringWithFormat:@"%@报警为%ld次", item.ptDes, (long)item.alarm_times];
        [mArray addObject:temp];
    }
    NSString *alarmNumStr = [mArray componentsJoinedByString:@"，"];
    if (self.alarm_total.count) {
        return [NSString stringWithFormat:@"%@：%@。", str, alarmNumStr];
    }
    return [NSString stringWithFormat:@"%@。", str];
}
- (NSCalendar *)calendar {
    if (!_calendar) {
        _calendar = [NSCalendar currentCalendar];
    }
    return _calendar;
}
- (NSArray<NSDictionary *> *)sortAlarmDataWithType:(NSInteger)type {
    NSMutableArray *mArray = [NSMutableArray array];
    NSDate *startDate = [self.start_time xd_yyyyMMddHHmmssStrToDate];
    if (type == 0) {            // 日报
        NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitHour inUnit:NSCalendarUnitDay forDate:startDate];
        NSDateComponents *com = [[NSDateComponents alloc] init];
        for (NSInteger i = 0; i < range.length; i++) {
            com.hour = i;
            
            NSDate *beginDate = [self.calendar dateByAddingComponents:com toDate:startDate options:NSCalendarMatchStrictly];
            NSInteger cnt = 0;
            for (EFARptAlarmDataItem *item in self.alarm_data_list) {
                NSDate *itemDate = [item.efairydevicefiredata_time xd_yyyyMMddHHmmssStrToDate];
                NSInteger interval = [itemDate timeIntervalSinceDate:beginDate];
                if ((interval >=0 ) && (interval < 60 * 60)) {
                    ++cnt;
                }
            }
            [mArray addObject:@{@"date" : beginDate, @"number" : @(cnt)}];
        }
    } else if (type == 1) {     // 周报
//        NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekday forDate:startDate];
        NSDateComponents *com = [[NSDateComponents alloc] init];
        for (NSInteger i = 0; i < 7; i++) {
            com.day = i;
            
            NSDate *beginDate = [self.calendar dateByAddingComponents:com toDate:startDate options:NSCalendarMatchStrictly];
            NSInteger cnt = 0;
            for (EFARptAlarmDataItem *item in self.alarm_data_list) {
                NSDate *itemDate = [item.efairydevicefiredata_time xd_yyyyMMddHHmmssStrToDate];
                NSInteger interval = [itemDate timeIntervalSinceDate:beginDate];
                if ((interval >=0 ) && (interval < 24 * 60 * 60)) {
                    ++cnt;
                }
            }
            [mArray addObject:@{@"date" : beginDate, @"number" : @(cnt)}];
        }
    } else if (type == 2) {     // 月报
        NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:startDate];
        NSDateComponents *com = [[NSDateComponents alloc] init];
        for (NSInteger i = 0; i < range.length; i++) {
            com.day = i;
            
            NSDate *beginDate = [self.calendar dateByAddingComponents:com toDate:startDate options:NSCalendarMatchStrictly];
            NSInteger cnt = 0;
            for (EFARptAlarmDataItem *item in self.alarm_data_list) {
                NSDate *itemDate = [item.efairydevicefiredata_time xd_yyyyMMddHHmmssStrToDate];
                NSInteger interval = [itemDate timeIntervalSinceDate:beginDate];
                if ((interval >=0 ) && (interval < 24 * 60 * 60)) {
                    ++cnt;
                }
            }
            [mArray addObject:@{@"date" : beginDate, @"number" : @(cnt)}];
        }
    } else if (type == 3) {     // 季报
        NSDateComponents *com = [[NSDateComponents alloc] init];
        for (NSInteger i = 0; i < 3; i++) {
            com.month = i;
            
            NSDate *beginDate = [self.calendar dateByAddingComponents:com toDate:startDate options:NSCalendarMatchStrictly];
            NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:beginDate];
            NSInteger cnt = 0;
            for (EFARptAlarmDataItem *item in self.alarm_data_list) {
                NSDate *itemDate = [item.efairydevicefiredata_time xd_yyyyMMddHHmmssStrToDate];
                NSInteger interval = [itemDate timeIntervalSinceDate:beginDate];
                if ((interval >=0 ) && (interval < range.length * 24 * 60 * 60)) {
                    ++cnt;
                }
            }
            [mArray addObject:@{@"date" : beginDate, @"number" : @(cnt)}];
        }
    } else {                    // 年报
        NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:startDate];
        NSDateComponents *com = [[NSDateComponents alloc] init];
        for (NSInteger i = 0; i < range.length; i++) {
            com.month = i;
            
            NSDate *beginDate = [self.calendar dateByAddingComponents:com toDate:startDate options:NSCalendarMatchStrictly];
            NSRange dayRange = [self.calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:beginDate];
            NSInteger cnt = 0;
            for (EFARptAlarmDataItem *item in self.alarm_data_list) {
                NSDate *itemDate = [item.efairydevicefiredata_time xd_yyyyMMddHHmmssStrToDate];
                NSInteger interval = [itemDate timeIntervalSinceDate:beginDate];
                if ((interval >=0 ) && (interval < dayRange.length * 24 * 60 * 60)) {
                    ++cnt;
                }
            }
            [mArray addObject:@{@"date" : beginDate, @"number" : @(cnt)}];
        }
    }
    
    return mArray;
}
@end


#pragma mark - 公告
@implementation EFANotPubItem
// 如果实现了该方法，则处理过程中会忽略该列表内的所有属性
+ (NSArray *)modelPropertyBlacklist {
    return @[@"img", @"originalData"];
}
- (instancetype)initWithImage:(UIImage *)img originalData:(NSData *)data {
    self = [super init];
    if (self) {
        _img = img;
        _originalData = data;
    }
    return self;
}
@end

@implementation EFANotInfo
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairyannouncement_image_list" : [EFANotPubItem class]};
}
- (instancetype)init {
    self = [super init];
    if (self) {
        _efairyannouncement_title = @"";
        _efairyannouncement_content = @"";
        _efairyannouncement_image_list = [NSMutableArray array];
    }
    return self;
}
@end

@implementation EFANoticePubModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"project_list" : [EFAPjtListItem class]};
}
- (instancetype)init {
    self = [super init];
    if (self) {
        _visibility_mode = 1;
        _project_list = [NSMutableArray array];
        _efairyannouncement_info = [[EFANotInfo alloc] init];
    }
    return self;
}
- (BOOL)isImgUpload {
    for (EFANotPubItem *item in self.efairyannouncement_info.efairyannouncement_image_list) {
        if (item.image_url.length == 0) {
            return NO;
        }
    }
    return YES;
}
- (NSArray<UIImage *> *)imgArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.efairyannouncement_info.efairyannouncement_image_list.count];
    [self.efairyannouncement_info.efairyannouncement_image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.img];
    }];
    return mArray;
}
- (NSArray<NSNumber *> *)pjtIdList {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.project_list.count];
    [self.project_list enumerateObjectsUsingBlock:^(EFAPjtListItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:@(obj.efairyproject_id)];
    }];
    return mArray;
}
- (NSArray<NSString *> *)urlArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.efairyannouncement_info.efairyannouncement_image_list.count];
    [self.efairyannouncement_info.efairyannouncement_image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.image_url];
    }];
    return mArray;
}
- (void)addPublishItemsWithImgArray:(NSArray<UIImage *> *)imgArray originalDataArray:(NSArray<NSData *> *)dataArray {
    [imgArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *originalData = nil;
        if (idx < dataArray.count) {
            originalData = dataArray[idx];
        }
        EFANotPubItem *item = [[EFANotPubItem alloc] initWithImage:obj originalData:originalData];
        [self.efairyannouncement_info.efairyannouncement_image_list addObject:item];
    }];
}
- (void)removePublishItemAtIndex:(NSInteger)index {
    if (index < self.efairyannouncement_info.efairyannouncement_image_list.count) {
        [self.efairyannouncement_info.efairyannouncement_image_list removeObjectAtIndex:index];
    }
}
- (void)updateImgUrlWithArray:(NSArray<NSString *> *)array {
    if (array.count != self.efairyannouncement_info.efairyannouncement_image_list.count) {
        return;
    }
    
    for (NSInteger i = 0; i < self.efairyannouncement_info.efairyannouncement_image_list.count; ++i) {
        EFANotPubItem *item = self.efairyannouncement_info.efairyannouncement_image_list[i];
        item.image_url = array[i];
    }
}
@end


@implementation EFANotImgItem
@end
@implementation EFANotListItem
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairyannouncement_image_list" : [EFANotImgItem class]};
}
@end
@implementation EFANoticeListModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"announcement_list" : [EFANotListItem class]};
}
- (BOOL)totalLoad {
    return (self.announcement_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.announcement_list.count == 0;
}
- (void)addMoreWithData:(EFANoticeListModel *)temp {
    self.total_rows = temp.total_rows;
    [self.announcement_list addObjectsFromArray:temp.announcement_list];
}
- (void)removeNotListItem:(EFANotListItem *)item {
    if (item) {
        [self.announcement_list removeObject:item];
        self.total_rows--;
    }
}
@end


#pragma mark - 维护记录
@implementation EFAMtcPubModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _efairymtrecord_title = @"";
        _image_list = [NSMutableArray array];
        _imgMaxCnt = 9;
    }
    return self;
}
- (void)setEfairymtrecord_mt_type_id:(NSInteger)efairymtrecord_mt_type_id {
    if (_efairymtrecord_mt_type_id == efairymtrecord_mt_type_id) {
        return;
    }
    _efairymtrecord_mt_type_id = efairymtrecord_mt_type_id;
    _efairymtreord_mt_handle_id = 0;
}
- (NSString *)mt_type_name {
    return [[EFAMtcTypeModel sharedInstance] mtcTypeNameWithMtcTypeId:self.efairymtrecord_mt_type_id];
}
- (NSString *)mt_handle_title {
    return [[EFAMtcTypeModel sharedInstance] mtcResultTitleWithMtcTypeId:self.efairymtrecord_mt_type_id];
}
- (NSString *)mt_handle_name {
    return [[EFAMtcTypeModel sharedInstance] mtcResultNameWithMtcResultId:self.efairymtreord_mt_handle_id];
}
- (BOOL)isEmpty {
    return !(self.efairymtrecord_content.length || self.image_list.count);
}
- (BOOL)isImgUpload {
    for (EFANotPubItem *item in self.image_list) {
        if (item.image_url.length == 0) {
            return NO;
        }
    }
    return YES;
}
- (NSArray<UIImage *> *)imgArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.img];
    }];
    return mArray;
}
- (NSArray<NSString *> *)urlArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.image_url];
    }];
    return mArray;
}
- (void)addPublishItemsWithImgArray:(NSArray<UIImage *> *)imgArray originalDataArray:(NSArray<NSData *> *)dataArray {
    [imgArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *originalData = nil;
        if (idx < dataArray.count) {
            originalData = dataArray[idx];
        }
        EFANotPubItem *item = [[EFANotPubItem alloc] initWithImage:obj originalData:originalData];
        [self.image_list addObject:item];
    }];
}
- (void)removePublishItemAtIndex:(NSInteger)index {
    if (index < self.image_list.count) {
        [self.image_list removeObjectAtIndex:index];
    }
}
- (void)updateImgUrlWithArray:(NSArray<NSString *> *)array {
    if (array.count != self.image_list.count) {
        return;
    }
    
    for (NSInteger i = 0; i < self.image_list.count; ++i) {
        EFANotPubItem *item = self.image_list[i];
        item.image_url = array[i];
    }
}
@end

@implementation EFAMtcListItem
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairymtrecord_imgurl_list" : [NSString class]};
}
- (NSString *)mt_type_name {
    return [[EFAMtcTypeModel sharedInstance] mtcTypeNameWithMtcTypeId:self.efairymtrecord_mt_type_id];
}
- (NSString *)mt_handle_title {
    return [[EFAMtcTypeModel sharedInstance] mtcResultTitleWithMtcTypeId:self.efairymtrecord_mt_type_id];
}
- (NSString *)mt_handle_name {
    return [[EFAMtcTypeModel sharedInstance] mtcResultNameWithMtcResultId:self.efairymtreord_mt_handle_id];
}
@end
@implementation EFAMtcRcdListModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"mtrecord_list" : [EFAMtcListItem class]};
}
- (BOOL)totalLoad {
    return (self.mtrecord_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.mtrecord_list.count == 0;
}
- (void)addMoreWithData:(EFAMtcRcdListModel *)temp {
    self.total_rows = temp.total_rows;
    [self.mtrecord_list addObjectsFromArray:temp.mtrecord_list];
}
- (void)removeNotListItem:(EFAMtcListItem *)item {
    if (item) {
        [self.mtrecord_list removeObject:item];
        self.total_rows--;
    }
}
@end

NSString *const EFAMtcTypeId = @"mtcTypeId";            //!< 维护类型ID
NSString *const EFAMtcTypeName = @"mtcTypeName";        //!< 维护类型Name
NSString *const EFAMtcResultTitle = @"mtcResultTitle";  //!< 维护结果标题
NSString *const EFAMtcResultId = @"mtcResultId";        //!< 是否完成处理类型ID
NSString *const EFAMtcResultName = @"mtcResultName";    //!< 是否完成处理类型Name
@implementation EFAMtcTypeModel
- (NSArray *)typeArray {
    if (!_typeArray) {
        _typeArray = @[@{@"mtcTypeId" : @(4),
                         @"mtcTypeName" : @"常规巡检",
                         @"mtcResultTitle" : @"巡检结果",
                         @"mtcResult" : @[@{@"mtcResultId" : @(7), @"mtcResultName" : @"正常"},
                                          @{@"mtcResultId" : @(8), @"mtcResultName" : @"异常"}]},
                       @{@"mtcTypeId" : @(1),
                         @"mtcTypeName" : @"产品故障",
                         @"mtcResultTitle" : @"故障是否排除",
                         @"mtcResult" : @[@{@"mtcResultId" : @(1), @"mtcResultName" : @"故障已排除"},
                                          @{@"mtcResultId" : @(2), @"mtcResultName" : @"故障待排除"}]},
                       @{@"mtcTypeId" : @(2),
                         @"mtcTypeName" : @"通讯故障",
                         @"mtcResultTitle" : @"故障是否排除",
                         @"mtcResult" : @[@{@"mtcResultId" : @(3), @"mtcResultName" : @"故障已排除"},
                                          @{@"mtcResultId" : @(4), @"mtcResultName" : @"故障待排除"}]},
                       @{@"mtcTypeId" : @(3),
                         @"mtcTypeName" : @"其它",
                         @"mtcResultTitle" : @"故障是否排除",
                         @"mtcResult" : @[@{@"mtcResultId" : @(5), @"mtcResultName" : @"故障已排除"},
                                          @{@"mtcResultId" : @(6), @"mtcResultName" : @"故障待排除"}]}];
    }
    return _typeArray;
}
+ (instancetype)sharedInstance {
    static EFAMtcTypeModel *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
- (NSArray *)resultTypeWithMtcTypeId:(NSInteger)mtcTypeId {
    for (NSDictionary *dic in self.typeArray) {
        if ([dic[EFAMtcTypeId] integerValue] == mtcTypeId) {
            return dic[@"mtcResult"];
        }
    }
    
    return nil;
}
- (NSString *)mtcTypeNameWithMtcTypeId:(NSInteger)mtcTypeId {
    for (NSDictionary *dic in self.typeArray) {
        if ([dic[EFAMtcTypeId] integerValue] == mtcTypeId) {
            return dic[EFAMtcTypeName];
        }
    }
    
    return nil;
}
- (NSString *)mtcResultTitleWithMtcTypeId:(NSInteger)mtcTypeId {
    for (NSDictionary *dic in self.typeArray) {
        if ([dic[EFAMtcTypeId] integerValue] == mtcTypeId) {
            return dic[EFAMtcResultTitle];
        }
    }
    
    return @"维护结果";
}
- (NSString *)mtcResultNameWithMtcResultId:(NSInteger)mtcResultId {
    for (NSDictionary *dic in self.typeArray) {
        for (NSDictionary *temp in dic[@"mtcResult"]) {
            if ([temp[EFAMtcResultId] integerValue] == mtcResultId) {
                return temp[EFAMtcResultName];
            }
        }
    }
    
    return nil;
}
@end


#pragma mark - 报警处理
@implementation EFAAlarmPubModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _image_list = [NSMutableArray array];
        _imgMaxCnt = 9;
    }
    return self;
}
//- (void)setEfairymtrecord_mt_type_id:(NSInteger)efairymtrecord_mt_type_id {
//    if (_efairymtrecord_mt_type_id == efairymtrecord_mt_type_id) {
//        return;
//    }
//    _efairymtrecord_mt_type_id = efairymtrecord_mt_type_id;
//    _efairymtreord_mt_handle_id = 0;
//}
//- (NSString *)mt_type_name {
//    return [[EFAMtcTypeModel sharedInstance] mtcTypeNameWithMtcTypeId:self.efairymtrecord_mt_type_id];
//}
//- (NSString *)mt_handle_name {
//    return [[EFAMtcTypeModel sharedInstance] mtcResultNameWithMtcResultId:self.efairymtreord_mt_handle_id];
//}
- (BOOL)isEmpty {
    return !(self.efairyalarmrecord_content.length || self.image_list.count);
}
- (BOOL)isImgUpload {
    for (EFANotPubItem *item in self.image_list) {
        if (item.image_url.length == 0) {
            return NO;
        }
    }
    return YES;
}
- (NSArray<UIImage *> *)imgArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.img];
    }];
    return mArray;
}
- (NSArray<NSString *> *)urlArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.image_url];
    }];
    return mArray;
}
- (void)addPublishItemsWithImgArray:(NSArray<UIImage *> *)imgArray originalDataArray:(NSArray<NSData *> *)dataArray {
    [imgArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *originalData = nil;
        if (idx < dataArray.count) {
            originalData = dataArray[idx];
        }
        EFANotPubItem *item = [[EFANotPubItem alloc] initWithImage:obj originalData:originalData];
        [self.image_list addObject:item];
    }];
}
- (void)removePublishItemAtIndex:(NSInteger)index {
    if (index < self.image_list.count) {
        [self.image_list removeObjectAtIndex:index];
    }
}
- (void)updateImgUrlWithArray:(NSArray<NSString *> *)array {
    if (array.count != self.image_list.count) {
        return;
    }
    
    for (NSInteger i = 0; i < self.image_list.count; ++i) {
        EFANotPubItem *item = self.image_list[i];
        item.image_url = array[i];
    }
}
@end

@implementation EFAAlarmListItem
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairyalarmrecord_imgurl_list" : [NSString class]};
}
@end
@implementation EFAAlarmRcdListModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"alarmrecord_list" : [EFAAlarmListItem class]};
}
- (BOOL)totalLoad {
    return (self.alarmrecord_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.alarmrecord_list.count == 0;
}
- (instancetype)initWithAlarmListItem:(EFAAlarmListItem *)item {
    self = [super init];
    if (self) {
        _total_rows = 1;
        _alarmrecord_list = [NSMutableArray arrayWithObject:item];
    }
    return self;
}
- (void)addMoreWithData:(EFAAlarmRcdListModel *)temp {
    self.total_rows = temp.total_rows;
    [self.alarmrecord_list addObjectsFromArray:temp.alarmrecord_list];
}
- (void)removeNotListItem:(EFAAlarmListItem *)item {
    if (item) {
        [self.alarmrecord_list removeObject:item];
        self.total_rows--;
    }
}
@end

@implementation EFAAlarmDevListItem
@end
@implementation EFAAlarmDevListModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"total_rows": @[@"total_rows", @"efairyproject_total_devices"]};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"unhandle_device_list" : [EFAAlarmDevListItem class]};
}
- (BOOL)totalLoad {
    return (self.unhandle_device_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.unhandle_device_list.count == 0;
}
- (void)addMoreWithData:(EFAAlarmDevListModel *)temp {
    self.total_rows = temp.total_rows;
    [self.unhandle_device_list addObjectsFromArray:temp.unhandle_device_list];
}
@end

#pragma mark - 隐患曝光
@implementation EFADangerPubModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _image_list = [NSMutableArray array];
        _imgMaxCnt = 9;
    }
    return self;
}
//- (void)setEfairymtrecord_mt_type_id:(NSInteger)efairymtrecord_mt_type_id {
//    if (_efairymtrecord_mt_type_id == efairymtrecord_mt_type_id) {
//        return;
//    }
//    _efairymtrecord_mt_type_id = efairymtrecord_mt_type_id;
//    _efairymtreord_mt_handle_id = 0;
//}
//- (NSString *)mt_type_name {
//    return [[EFAMtcTypeModel sharedInstance] mtcTypeNameWithMtcTypeId:self.efairymtrecord_mt_type_id];
//}
//- (NSString *)mt_handle_name {
//    return [[EFAMtcTypeModel sharedInstance] mtcResultNameWithMtcResultId:self.efairymtreord_mt_handle_id];
//}
- (BOOL)isEmpty {
    return !(self.efairyhiddendanger_content.length || self.image_list.count || self.efairyhiddendanger_title.length);
}
- (BOOL)isImgUpload {
    for (EFANotPubItem *item in self.image_list) {
        if (item.image_url.length == 0) {
            return NO;
        }
    }
    return YES;
}
- (NSArray<UIImage *> *)imgArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.img];
    }];
    return mArray;
}
- (NSArray<NSString *> *)urlArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.image_url];
    }];
    return mArray;
}
- (void)addPublishItemsWithImgArray:(NSArray<UIImage *> *)imgArray originalDataArray:(NSArray<NSData *> *)dataArray {
    [imgArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *originalData = nil;
        if (idx < dataArray.count) {
            originalData = dataArray[idx];
        }
        EFANotPubItem *item = [[EFANotPubItem alloc] initWithImage:obj originalData:originalData];
        [self.image_list addObject:item];
    }];
}
- (void)removePublishItemAtIndex:(NSInteger)index {
    if (index < self.image_list.count) {
        [self.image_list removeObjectAtIndex:index];
    }
}
- (void)updateImgUrlWithArray:(NSArray<NSString *> *)array {
    if (array.count != self.image_list.count) {
        return;
    }
    
    for (NSInteger i = 0; i < self.image_list.count; ++i) {
        EFANotPubItem *item = self.image_list[i];
        item.image_url = array[i];
    }
}
@end

@implementation EFADangerListItem
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"efairyhiddendanger_imgurl_list" : [NSString class]};
}
@end
@implementation EFADangerListModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"hiddendanger_list" : [EFADangerListItem class]};
}
- (instancetype)initWithDangerListItem:(EFADangerListItem *)item {
    self = [super init];
    if (self) {
        _total_rows = 1;
        _hiddendanger_list = [NSMutableArray arrayWithObject:item];
    }
    return self;
}
- (BOOL)totalLoad {
    return (self.hiddendanger_list.count >= self.total_rows);
}
- (BOOL)totalZero {
    return self.hiddendanger_list.count == 0;
}
- (void)addMoreWithData:(EFADangerListModel *)temp {
    self.total_rows = temp.total_rows;
    [self.hiddendanger_list addObjectsFromArray:temp.hiddendanger_list];
}
@end


#pragma mark - 用户反馈
@implementation EFAFeedbackModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _image_list = [NSMutableArray array];
        _imgMaxCnt = 3;
    }
    return self;
}
- (BOOL)isEmpty {
    return !(self.efairyfeedback_content.length || self.image_list.count);
}
- (BOOL)isImgUpload {
    for (EFANotPubItem *item in self.image_list) {
        if (item.image_url.length == 0) {
            return NO;
        }
    }
    return YES;
}
- (NSArray<UIImage *> *)imgArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.img];
    }];
    return mArray;
}
- (NSArray<NSString *> *)urlArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.image_list.count];
    [self.image_list enumerateObjectsUsingBlock:^(EFANotPubItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.image_url];
    }];
    return mArray;
}
- (void)addPublishItemsWithImgArray:(NSArray<UIImage *> *)imgArray originalDataArray:(NSArray<NSData *> *)dataArray {
    [imgArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *originalData = nil;
        if (idx < dataArray.count) {
            originalData = dataArray[idx];
        }
        EFANotPubItem *item = [[EFANotPubItem alloc] initWithImage:obj originalData:originalData];
        [self.image_list addObject:item];
    }];
}
- (void)removePublishItemAtIndex:(NSInteger)index {
    if (index < self.image_list.count) {
        [self.image_list removeObjectAtIndex:index];
    }
}
- (void)updateImgUrlWithArray:(NSArray<NSString *> *)array {
    if (array.count != self.image_list.count) {
        return;
    }
    
    for (NSInteger i = 0; i < self.image_list.count; ++i) {
        EFANotPubItem *item = self.image_list[i];
        item.image_url = array[i];
    }
}
@end


#pragma mark - 暂时无用
@implementation EFAAppModel
@end

